package com.drinks.yiniang.ui.activity.user.wallet;

import android.content.Intent;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.StyleSpan;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.drinks.yiniang.R;
import com.drinks.yiniang.entity.user.wallet.WalletInfoJson;
import com.drinks.yiniang.io.api.UserApiIO;
import com.drinks.yiniang.ui.activity.conversation.ServiceChatActivity;
import com.drinks.yiniang.ui.activity.user.setting.PaymentSettingActivity;
import com.drinks.yiniang.utils.ToastUtils;
import com.hansen.library.Constants;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.ui.activity.BaseTranBarActivity;
import com.hansen.library.listener.OnNavgationBarClickListener;
import com.hansen.library.ui.widget.nav.NavigationBarLayout;
import com.hansen.library.utils.ScreenSizeUtils;
import com.hansen.library.utils.StringUtils;

/**
 * 钱包页面
 *
 * @author han
 * @date 2019/09/06
 */
public class WalletActivity extends BaseTranBarActivity implements OnNavgationBarClickListener {

    private final int REQ_RECHARGE_CODE = 0x01;//充值
    private final int REQ_WIDTHDRAW_CODE = 0x02;//提现

    //nav bar
    private NavigationBarLayout nav_bar_wallet;
    //余额
    private TextView tv_wallet_balance;
    //可消费金额
    private TextView tv_wallet_can_use_balance;
    //可提现金额
    private TextView tv_wallet_can_withdraw_cash;
    //联系客服
    private TextView tv_wallet_contact_service;
    //充值
    private Button btn_wallet_recharge;
    //提现
    private Button btn_wallet_withdraw;
    //支付设置
    private Button btn_wallet_pay_set;

    private SpannableStringBuilder mBuilder;

    private boolean isNeedSetResult;

    @Override
    protected void initView() {
        nav_bar_wallet = findViewById(R.id.nav_bar_wallet);

        tv_wallet_balance = findViewById(R.id.tv_wallet_balance);
        tv_wallet_can_use_balance = findViewById(R.id.tv_wallet_can_use_balance);
        tv_wallet_can_withdraw_cash = findViewById(R.id.tv_wallet_can_withdraw_cash);
        tv_wallet_contact_service = findViewById(R.id.tv_wallet_contact_service);

        btn_wallet_recharge = findViewById(R.id.btn_wallet_recharge);
        btn_wallet_withdraw = findViewById(R.id.btn_wallet_withdraw);
        btn_wallet_pay_set = findViewById(R.id.btn_wallet_pay_set);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mContext = this;

        mBuilder = new SpannableStringBuilder();

        getWalletInfo();
    }

    @Override
    protected void initListener() {
        nav_bar_wallet.setOnNavgationBarClickListener(this);
        btn_wallet_recharge.setOnClickListener(this);
        btn_wallet_withdraw.setOnClickListener(this);
        btn_wallet_pay_set.setOnClickListener(this);
        tv_wallet_contact_service.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View v) {
        Intent mIntent;

        switch (v.getId()) {
            case R.id.btn_wallet_recharge://充值
                mIntent = new Intent(mContext, RechargeActivity.class);
                startActivityForResult(mIntent, REQ_RECHARGE_CODE);
                break;
            case R.id.btn_wallet_withdraw://提现
                mIntent = new Intent(mContext, DrawCashActivity.class);
                startActivityForResult(mIntent, REQ_WIDTHDRAW_CODE);
                break;
            case R.id.btn_wallet_pay_set://支付设置
                mIntent = new Intent(mContext, PaymentSettingActivity.class);
                startActivity(mIntent);
                break;
            case R.id.tv_wallet_contact_service://；联系客服
                Intent intent = new Intent(mContext, ServiceChatActivity.class);
                intent.putExtra(Constants.KeyBoolen, true);
                intent.putExtra(Constants.KeyTitle, getString(R.string.text_customer_service));
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    @Override
    public int getTranBarResID() {
        return R.layout.activity_wallet;
    }

    @Override
    public void onBackClick(View v) {
        doFinish();
    }

    @Override
    public void onEditClick(View v) {

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            doFinish();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Constants.RESULT_CODE_NORMAL) {
            switch (requestCode) {
                case REQ_RECHARGE_CODE://充值
                    isNeedSetResult = true;
                    getWalletInfo();
                    break;
                case REQ_WIDTHDRAW_CODE://提现
                    getWalletInfo();
                    break;
                default:
                    break;
            }
        }
    }

    private void doFinish() {
        if(isNeedSetResult) {
            setResult(Constants.RESULT_CODE_NORMAL);
        }

        finish();
    }

    //获取钱包信息
    private void getWalletInfo() {
        showLoadingDialog();

        UserApiIO.getInstance().getWalletInfo(new APIRequestCallback<WalletInfoJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(WalletInfoJson res) {
                setWalletData(res.getData());
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }

    private void setWalletData(WalletInfoJson.WalletInfoData data) {
        //总金额
        tv_wallet_balance.setText(StringUtils.getNullEmptyConvertZeroPrice(data.getCoin_balance()));

        //可消费金额
        mBuilder.clearSpans();
        mBuilder.clear();
        mBuilder.append("¥");
        mBuilder.append(StringUtils.getNullEmptyConvertZeroPrice(data.getCoin_balance()));
        tv_wallet_can_use_balance.setText(mBuilder);

        //可提现金额
        mBuilder.clearSpans();
        mBuilder.clear();
        mBuilder.append("¥");
        mBuilder.append(StringUtils.getNullEmptyConvertZeroPrice(data.getGain_balance()));
        tv_wallet_can_withdraw_cash.setText(mBuilder);
    }
}
